package cn.bdqn.controller;
import cn.bdqn.domain.Notice;
import cn.bdqn.service.NoticeService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 通知公告控制层
 * @author dkj
 */
@RestController
@RequestMapping(value = "/Notice")
public class NoticeController {

    @Autowired
     private NoticeService noticeService;

    /**
     * 分页查询
     * @param pageNum 当前页
     * @return
     */
    @RequestMapping("/queryAllPage")
    public Result queryAllPage(@RequestParam(defaultValue = "1") Integer pageNum, Integer pageSize)throws Exception{
        try {
            //获取封装对象
            Page page = noticeService.queryAllNotice(pageNum,pageSize);
            //返回结果 page.getMenuList() 分页后的数据  page.getTotalRecord()总数
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
    /**
     * 根据时间排序分页查询
     * @param pageNum 当前页
     * @return
     */
    @RequestMapping("/queryByIssueTimePage")
    public Result queryByIssueTimePage(@RequestParam(defaultValue = "1") Integer pageNum)throws Exception{
        try {
            //获取封装对象
            Page page = noticeService.queryBYIssueTime(pageNum);
            //返回结果 page.getMenuList() 分页后的数据  page.getTotalRecord()总数
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 添加一个通知公告
     * @param notice
     * @return
     */
    @RequestMapping("/saveNotice")
    public Result saveNotice(Notice notice)throws Exception{
        try{
            noticeService.saveNotice(notice);
            return ResultGenerator.genSuccessResult();
    }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
       }
    }

    /**
     * 修改数据(点赞数,收藏数量,浏览数量,发布状态)
     * @param notice
     * @return
     */
    @RequestMapping("/changeNotice")
    public Result changeNotice(Notice notice)throws Exception{
        try {
            noticeService.changeNotice(notice);
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }



}
